<?php
// Core class for YAPP Framework
// By: Nicolas Grecco

class Core
{

    // Vars
    public $modules;
    public $currentModule;
    public $currentModuleName;
    public $currentAction;

    // Own
    private $conf;
    private $controller;
    private $model;
    private $view;

    // Class constructor
    public function __construct(&$conf)
    {
        $this->conf    = $conf;
        $this->modules = new Modules();
        $this->getModuleAndAction();
        $this->instanceSingletonClasses();
        $this->instanceModule();
        $this->linkModule();
    }

    // Get module and action for url
    private function getModuleAndAction()
    {
        $url = $_SERVER['REQUEST_URI'];
        $url = strip_tags($url);
        if ((!empty($url)) && ($url != '/'))
        {
            $url = explode('/', substr($url, 1, strlen($url)-1));
            if ($module = $this->modules->getModule($url[0]))
            {
                $this->currentModule = $module;
                $this->currentModuleName = $this->currentModule['module_name'];
                if (!empty($url[1]) && ($url[1] != '/'))
                {
                    if ($action = $this->modules->getAction($this->currentModuleName, $url[1]))
                    {
                        $this->currentAction = $action;
                    }
                    else
                    {
                        $this->currentAction = $this->modules->getDefaultAction($this->currentModuleName);
                    }
                }
                else
                {
                    $this->currentAction = $this->modules->getDefaultAction($this->currentModuleName);
                }
            }
            else
            {
                $this->currentModule     = $this->modules->getDefaultModule();
                $this->currentModuleName = $this->currentModule['module_name'];
                $this->currentAction     = $this->modules->getDefaultAction($this->currentModuleName);
            }
        }
        else
        {
            $this->currentModule     = $this->modules->getDefaultModule();
            $this->currentModuleName = $this->currentModule['module_name'];
            $this->currentAction     = $this->modules->getDefaultAction($this->currentModuleName);
        }
        $this->modules->setCurrentModuleAndAction($this->currentModuleName, $this->currentAction);
    }

    // Instance module
    private function instanceModule()
    {
        if (file_exists($this->currentModule['module_path']))
        {
            if (file_exists($this->currentModule['module_path'].'/'.$this->currentModule['controller'].'.php'))
            {
                require_once($this->currentModule['module_path'].'/'.$this->currentModule['controller'].'.php');
                $this->controller = new $this->currentModule['controller']();
            }
            else
            {
                die("CONTROLLER DOES NOT EXIST!");
            }
            if (file_exists($this->currentModule['module_path'].'/'.$this->currentModule['model'].'.php'))
            {
                require_once($this->currentModule['module_path'].'/'.$this->currentModule['model'].'.php');
                $this->model = new $this->currentModule['model']();
            }
            else
            {
                die("MODEL DOES NOT EXIST!");
            }
            if (file_exists($this->currentModule['module_path'].'/'.$this->currentModule['view'].'.php'))
            {
                require_once($this->currentModule['module_path'].'/'.$this->currentModule['view'].'.php');
                $this->view = new $this->currentModule['view']($this->conf['templates'], $this->currentModule['templates_path']);
            }
            else
            {
                die("VIEW DOES NOT EXIST!");
            }
        }
        else
        {
            die("MODULES DIRECTORY DOES NO EXISTS FOR THIS MODULE!");
        }

    }

    // Link module
    private function linkModule()
    {
        // Controller links
        $this->controller->linkModel($this->model);
        $this->controller->linkView($this->view);
        $this->controller->linkModules($this->modules);
        $this->controller->setConf($this->conf);
        // View links
        $this->view->linkController($this->controller);
    }

    // Instance all singleton classes that will be used by the module
    private function instanceSingletonClasses()
    {
        // Database
        if (!empty($this->conf['db']))
        {
            if (!empty($this->conf['db']['database_access']['engine']))
            {
                if ($wrapperName = yappDBWrapper::engineSupported($this->conf['db']['database_access']['engine']))
                {
                    $DB = genericDBWrapper::getInstance($wrapperName, $this->conf['db']);
                    $DB->setDatabase();
                }
                else
                {
                    die("Engine {$this->conf['db']['database_access']['engine']} is not supported!");
                }
            }
            else
            {
                die("No DB engine selected");
            }
        }
        else
        {
            die("No database configuration!");
        }
        // Session
        $session = yappSession::getInstance();
    }

    // Call action
    public function callAction()
    {
        if (method_exists($this->controller, $this->currentAction))
        {
            $this->controller->{$this->currentAction}();
        }
        else
        {
            die("ACTION IS NOT DEFINED IN CONTROLLER!");
        }
    }

    // Destruct
    public function __destruct()
    {
        // Close Database
        $db = genericDBWrapper::getInstance();
        $db->closeConnection();
        // WriteClose Session
        $session = yappSession::getInstance();
        $session->writeClose();
    }
}
?>